package com.skynet.supervision.utils;

import cn.hutool.core.util.ObjectUtil;
import com.skynet.supervision.aop.uac.UserBriefInfo;
import com.skynet.supervision.aop.uac.beans.UACUserInfo;
import com.skynet.supervision.constants.SysConstants;
import com.skynet.supervision.dao.UserAccountMapper;
import com.skynet.supervision.dao.UserInfosMapper;
import com.skynet.supervision.entities.UserAccount;
import com.skynet.supervision.entities.UserInfo;

public class UserInfoCacheUtils {
    public static final String SEPARATOR = ",";

    private static String getCacheName() {
        return SysConstants.SYS_USER_CACHE;
    }

    private static String getBriefInfoCacheKey(String configKey) {
        return SysConstants.SYS_USER_CACHE_KEY + configKey;
    }

    private static String getUACUserInfoCacheKey(String configKey) {
        return SysConstants.SYS_USER_CACHE_KEY + "uac_user:" + configKey;
    }

    public static void removeUserBriefCache(String uid) {
        CacheUtils.remove(getCacheName(), getBriefInfoCacheKey(uid));
    }

    public static void setUserBriefInfoCache(String uid, UserBriefInfo userBriefInfo) {
        CacheUtils.put(getCacheName(), getBriefInfoCacheKey(uid), userBriefInfo);
    }

    public static UserBriefInfo getUserBriefInfoCache(String uid) {
        Object cacheObj = CacheUtils.get(getCacheName(), getBriefInfoCacheKey(uid));
        if (ObjectUtil.isNotNull(cacheObj)) {
            return ObjectUtils.cast(cacheObj);
        }
        return null;
    }

    public static void removeUACUserInfoCache(String uid) {
        CacheUtils.remove(getCacheName(), getUACUserInfoCacheKey(uid));
    }

    public static void setUACUserInfoCache(String uid, UACUserInfo userInfo) {
        CacheUtils.put(getCacheName(), getUACUserInfoCacheKey(uid), userInfo);
    }

    public static UACUserInfo getUACUserInfoCache(String uid) {
        Object cacheObj = CacheUtils.get(getCacheName(), getUACUserInfoCacheKey(uid));
        if (ObjectUtil.isNotNull(cacheObj)) {
            return ObjectUtils.cast(cacheObj);
        }
        return null;
    }

    public static UACUserInfo getUACUserInfoCacheOrFromDb(UserAccountMapper accountMapper, UserInfosMapper userInfosMapper,String uid) {
        Object cacheObj = CacheUtils.get(getCacheName(), getUACUserInfoCacheKey(uid));
        if (ObjectUtil.isNotNull(cacheObj)) {
            return ObjectUtils.cast(cacheObj);
        }
        //load from db
        UserAccount account = accountMapper.getOneById(uid);
        UserInfo info = userInfosMapper.queryByUserId(account.getId());
        if (account != null) {
            UACUserInfo userInfo = UACUserInfo
                    .builder()
                    .userId(account.getId())
                    .account(account.getAccount())
                    .status(account.getStatus())
                    .email(info.getEmail())
                    .rongCloudToken(info.getRongCloudToken())
                    .name(account.getUserName())
                    .sex(account.getSex())
                    .avatar(account.getAvatar())
                    .lastLoginTime(account.getLastLoginAt())
                    .build();
            setUACUserInfoCache(uid, userInfo);
            return userInfo;
        }
        return null;
    }
}
